package club.guadazi.wemarket.controller.admin;

import club.guadazi.wemarket.entity.OrderSimpleInfo;
import club.guadazi.wemarket.service.OrderSimpleService;
import club.guadazi.wemarket.util.excel.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("management/order")
public class OrderController {

    @RequestMapping("/")
    public String management() {
        return "management";
    }

    @Resource
    private OrderSimpleService orderSimpleService;

    @RequestMapping(value = "")
    public String order(Model model,
                        @RequestParam(name = "userName", required = false) String userName,
                        @RequestParam(name = "postNo", required = false) String postNo) {
        OrderSimpleInfo orderSimpleInfo = new OrderSimpleInfo();
        orderSimpleInfo.setUserName(userName);
        orderSimpleInfo.setPostNo(postNo);
        List<OrderSimpleInfo> orderSimpleInfos = orderSimpleService.listAllLike(orderSimpleInfo);
        model.addAttribute("datas", orderSimpleInfos);
        model.addAttribute("userName", userName);
        model.addAttribute("postNo", postNo);
        return "manage/order/list";
    }

    @RequestMapping("downloadExcelTemplate.do")
    public void download(HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");

        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=order_simple_list.xls");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            List<String> excelHead = ExcelUtils.getExcelHead(OrderSimpleInfo.class);
            ExcelUtils.write2Excel(excelHead, null, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("toImport.do")
    public String toImport() throws IOException {
        return "manage/order/import";
    }


    @RequestMapping("import.do")
    public String upload(HttpServletRequest request, Model model) throws IOException {
        // 这里我用到了jar包
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        List<OrderSimpleInfo> result = new ArrayList<>();
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                MultipartFile file = multiRequest.getFile((String) iter.next());
                try {
                    List<OrderSimpleInfo> infos = orderSimpleService.importOrderSimpleInfos(file);
                    if (!infos.isEmpty()) {
                        result.addAll(infos);
                    }
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                    model.addAttribute("errorMsg", e.getMessage());
                }
            }
        }
        model.addAttribute("results", result);
        model.addAttribute("code", "0");

        return "manage/order/import_result";

    }

}
